package com.lydms.service.impl;


import com.lydms.dao.PageDao;
import com.lydms.pojo.Page;
import com.lydms.service.LearnSmsService;
import com.lydms.thirdparty.AliSms;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class LearnSmsServiceImpl implements LearnSmsService {
    private static final Logger logger = LogManager.getLogger(LearnSmsServiceImpl.class);

    private static String LYD = "李阳达";
    private static String LJJ = "刘金晶";

    @Value(value = "${morePage}")
    private int morePage;

    @Autowired
    private PageDao pageDao;

    @Autowired
    private AliSms aliSms;


    /**
     * 像数据库中插入页数
     *
     * @param page
     * @return
     */
    @Override
    public int addPage(Page page) {
//        1、数据的处理
        if (LYD.equals(page.getName())) {
            page.setNamecode("0");
        }
        if (LJJ.equals(page.getName())) {
            page.setNamecode("1");
        }
        page.setCreatedate(String.valueOf(new Date().getTime()));
        logger.info("插入数据库的数据为:{}",page.toString());
//        2、插入数据库
        int result = pageDao.insertSelective(page);
        logger.info("插入数据库的结果为:{}",result);
        return result;
    }

    /**
     * 获取每个人最新的页数
     *
     * @return
     */
    @Override
    public List<Page> getPage() {
        Page lydPage = pageDao.selectByName(LYD);
        Page ljjPage = pageDao.selectByName(LJJ);
        ArrayList<Page> pageList = new ArrayList<>();
        if (lydPage != null) {
            pageList.add(lydPage);
        }
        if (ljjPage != null) {
            pageList.add(ljjPage);
        }
        logger.info("从数据库查询出来的结果为:{}",pageList.toString());
        return pageList;
    }

    /**
     * 发送短信
     *
     * @param pageList
     * @return
     */
    @Override
    public List<String> sendSms(List<Page> pageList, String phones) {
        ArrayList<String> resultList = new ArrayList<>();
        String end = null;    //剩余天数
        String today = null;  //应看到页数
        String ly = null;    //李看到的页数
        String lj = null;    //刘看到的页数

//        1、计算剩余天数
        int endDay = endDay();
        if (endDay==0){
            return null;
        }
        end = String.valueOf(endDay);

//        2、应看到的天数
        int mustDay = mustDay();
        if (mustDay==0){
            return null;
        }
        today = String.valueOf(mustDay);

//        3、每个人应看到的页数
        String[] split = phones.split(",");
        for (String phoneOne : split) {
            for (Page page : pageList) {
                if (LYD.equals(page.getName())) {
                    ly = page.getPage().toString();
                } else if (LJJ.equals(page.getName())) {
                    lj = page.getPage().toString();
                }
            }

//            4、发送短信
            logger.info("发送短信传递的参数为" + "phoneOne: " + phoneOne + "end: " + end + "today: " + today + "ly: " + ly + "lj: " + lj);
            String result = aliSms.sendSms(phoneOne, end, today, ly, lj);
            logger.info("发送的手机号" + phoneOne + "的结果为" + result);
            resultList.add(result);
        }
        return resultList;
    }

    /**
     * 计算剩余天数
     *
     * @return
     */
    private int endDay() {
        int allTime = 23 + 30 + 31;
        int end = 0;
        GregorianCalendar calendar = new GregorianCalendar();
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (2 == month) {
            return 0;
        } else if (3 == month) {
            end = allTime - day;
        } else if (4 == month) {
            end = allTime - 31 - day;
        } else if (5 == month) {
            end = allTime - 31 - 30 - day;
        } else {
            end = 0;
        }
        return end;
    }

    /**
     * 应看到的天数
     *
     * @return
     */
    private int mustDay() {
        int today = 0;
        GregorianCalendar calendar = new GregorianCalendar();
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (2 == month) {
            return 0;
        } else if (3 == month) {
            today = day * 5 + morePage;
        } else if (4 == month) {
            today = (31 + day) * 5 + morePage;
        } else if (5 == month) {
            today = (31 + 30 + day) * 5 + morePage;
        } else {
            today = 0;
        }
        logger.info("应该看到的页数为" + today);
        return today;
    }
}
